﻿using System;
using System.Collections.Generic;

namespace Sustainalytics.Common.Hosting
{
    public class ServiceContainer : IDisposable
    {
        List<IDisposable> services = new List<IDisposable>();
        private bool _disposed = false;
        public List<IDisposable> Services
        {
            get { return services; }
            set { services = value; }
        }


        public ServiceContainer()
        {
        }

      
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing) 
        {
            if (this._disposed)
                return;
            
            this._disposed = true;

            if (disposing)
            {

                if (services != null)
                {
                    var servicesToDispose = services.ToArray();
                    this.services = null;

                    foreach (var service in servicesToDispose)
                    {
                        service.Dispose();
                    }
                }
            }            
        }
    }
}
